package org.devgframwork.datasync.core.support.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.devgframwork.datasync.core.entity.ScheduleConfig;
import org.devgframwork.datasync.core.support.config.SysConfig;
import org.devgframwork.datasync.core.support.dao.ScheduleConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ScheduleConfigService extends ServiceImpl<ScheduleConfigMapper, ScheduleConfig> {

    @Autowired
    private SysConfig sysConfig;

    @Autowired
    private ScheduleConfigMapper scheduleConfigMapper;

    public List<ScheduleConfig> getScheduleConfigList() {
        String evnName = sysConfig.getSchedule().getEvnName();
        QueryWrapper<ScheduleConfig> queryWrapper = new QueryWrapper();
        queryWrapper.eq("ENV_NAME", evnName);
        queryWrapper.eq("IS_DELETE", 0);
        return scheduleConfigMapper.selectList(queryWrapper);
    }

    public ScheduleConfig getScheduleConfigById(String id) {
        String evnName = sysConfig.getSchedule().getEvnName();
        ScheduleConfig scheduleConfigPo = scheduleConfigMapper.selectById(id);
        if (null == scheduleConfigPo || !scheduleConfigPo.getEvnName().equals(evnName)) {
            return null;
        }
        return scheduleConfigPo;

    }

    public Integer updateSyncNumById(Map<String, Object> map) {
        return scheduleConfigMapper.updateSyncNumById(map);
    }

    public Integer updateRunning() {
        String evnName = sysConfig.getSchedule().getEvnName();
        Boolean clearRunningState = sysConfig.getSchedule().getClearRunningState();
        if (!clearRunningState) {
            return 0;
        }
        return scheduleConfigMapper.updateRunning(evnName);
    }

}
